package com.lsw.view;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lsw.R;
import com.lsw.util.StringUtil;


/**
 * Created by lsw on 2017/7/26.
 * 自定义数量选择器
 */

public class SelectorNumber extends LinearLayout implements View.OnClickListener {
    private Context context;
    private TextView tv_add, tv_delect;
    private EditText tv_number;
    private int number = 1;
    private int max = 99;

    public void setMax(int max) {
        if (max > 99)
            return;
        this.max = max;
        if (max < number) {
            setNumber(max);
        }
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        if (number == 0) {
            this.number = 1;
        } else {
            this.number = number;
        }
        if (tv_number != null) {
            tv_number.setText(this.number + "");
        }
    }

    public SelectorNumber(Context context) {
        this(context, null);
    }

    public SelectorNumber(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SelectorNumber(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    public void init() {
        View view = LayoutInflater.from(context).inflate(R.layout.view_selector_number, null);
        tv_add = view.findViewById(R.id.tv_add);
        tv_delect = view.findViewById(R.id.tv_delect);
        tv_number = view.findViewById(R.id.tv_number);
        tv_add.setOnClickListener(this);
        tv_delect.setOnClickListener(this);
        tv_number.setText(number + "");
        addView(view);
        myToast = new MyToast(context);
        tv_number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!StringUtil.isNullOrEmpty(s.toString())) {
                    if (Integer.valueOf(s.toString()) > max) {
                        tv_number.setText(max + "");
                    }

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    MyToast myToast;

    @Override
    public void onClick(View v) {
        if (!StringUtil.isNullOrEmpty(tv_number.getText().toString())) {
            number = Integer.valueOf(tv_number.getText().toString());
        } else {
            number = 0;
        }
        int i = v.getId();
        if (i == R.id.tv_add) {
            if (number >= max) {
                //不能超过最大值
                myToast.Showgray("没有更多库存");
            } else {
                number++;
                tv_number.setText(number + "");
            }

        } else if (i == R.id.tv_delect) {
            if (number > 1) {
                number--;
                tv_number.setText(number + "");
            }

        }
    }
}
